#include "speechsynthesizer.h"

SpeechSynthesizer::SpeechSynthesizer(QObject *parent) : QObject(parent)
    ,synSession(nullptr)
{
}
SpeechSynthesizer::~SpeechSynthesizer()
{
    speech_synthesizer_destroy_session(&synSession);
}
bool SpeechSynthesizer::initialize()
{

    synSession =speech_synthesizer_create_session();
    if(!synSession){
        qWarning() << "创建会话失败";
        return false;
    }
    if(speech_synthesizer_init_session(synSession)!=0){
        qWarning() << "初始化失败";
        speech_synthesizer_destroy_session(&synSession);
        return false;
    }

    SpeechModelConfig *modelconfig = speech_model_config_create();
    speech_model_config_set_name(modelconfig, "百度-语音大模型 ");
    speech_model_config_set_deploy_type(modelconfig,ModelDeployType::PublicCloud);
    speech_synthesizer_set_model_config(synSession, modelconfig);

    auto *synthesizerConfig =audio_config_create_audio_output_from_default_speaker();
    audio_config_set_input_audio_rate(synthesizerConfig, 16000);    // 设置音频的格式
    audio_config_set_output_audio_speed(synthesizerConfig, 50);    // 设置输出音频的格式：语速，可选值：[0-100]，默认为50
    audio_config_set_output_audio_volume(synthesizerConfig, 50);    // 设置输出音频的格式：音量，可选值：[0-100]，默认为50
    audio_config_set_output_audio_pitch(synthesizerConfig, 50);    // 设置输出音频的格式：音高，可选值：[0-100]，默认为50
    speech_synthesizer_set_audio_config(synSession, synthesizerConfig);

    speech_synthesizer_result_set_callback(synSession, synthesisCallback,nullptr);
    return true;
}
int SpeechSynthesizer::SynthesisOutput(const char *text)
{
        speech_synthesizer_synthesize_text_async(synSession, text, 100);
        return speech_synthesizer_stop_speaking(synSession);
}


void SpeechSynthesizer::synthesisCallback(SpeechSynthesisResult* result, void* userData) {
    // 获取合成结果状态
    SpeechResultReason reason = speech_synthesis_result_get_reason(result);
    qDebug() << "Synthesis callback reason:" << reason;
}
